<?php
namespace App\Admin\Controller;

use Foxforms\Widget\Field\Radio;
use Foxforms\Widget\Field\Input;
use Foxforms\Widget\Form;

class configuracaoController extends \Foxframe\Controller
{
	public function index($params = array())
	{
		$this->redirect(BASE_PATH.'admin/configuracao/viewConfiguracao');
	}

	public function viewConfiguracao($params = array())
	{
		$configuracao	= $this->em->getRepository('\Entities\Corp\Configuracao')->findOneBy(array());
		if (!($configuracao instanceof \Entities\Corp\Configuracao))
			$configuracao	= new \Entities\Corp\Configuracao();

		$formFTP	= new Form();
		$formFTP->addValues($configuracao, 'configuracao');
		$formFTP->addElement(Input::create('application')->setType('hidden')->setValue('admin'));
		$formFTP->addElement(Input::create('controller')->setType('hidden')->setValue('configuracao'));
		$formFTP->addElement(Input::create('method')->setType('hidden')->setValue('save'));
		$formFTP->addElement(Input::create('configuracao[version]')->setType('hidden'));
		$formFTP->addElement(Input::create('configuracao[id_configuracao]')->setType('hidden'));
		$formFTP->addElement(Input::create('configuracao[ftp_host]')->setLabel('Host')->validate());
		$formFTP->addElement(Input::create('configuracao[ftp_porta]')->setLabel('Porta')->validate());
		$formFTP->addElement(Input::create('configuracao[ftp_usuario]')->setLabel('Usuario')->validate());
		$formFTP->addElement(Input::create('configuracao[ftp_senha]')->setLabel('Senha')->setType('password')->validate());
		$formFTP->addElement(Input::create('salvar')->setType('submit')->setValue('Salvar')->addClass(array('btn','btn-primary')));

		$formSMTP	= new Form();
		$formSMTP->addValues($configuracao, 'configuracao');
		$formSMTP->addElement(Input::create('application')->setType('hidden')->setValue('admin'));
		$formSMTP->addElement(Input::create('controller')->setType('hidden')->setValue('configuracao'));
		$formSMTP->addElement(Input::create('method')->setType('hidden')->setValue('save'));
		$formSMTP->addElement(Input::create('configuracao[version]')->setType('hidden'));
		$formSMTP->addElement(Input::create('configuracao[id_configuracao]')->setType('hidden'));
		$formSMTP->addElement(Input::create('configuracao[smtp_host]')->setLabel('Host')->validate());
		$formSMTP->addElement(Input::create('configuracao[smtp_porta]')->setLabel('Porta')->validate());
		$formSMTP->addElement(Input::create('configuracao[smtp_usuario]')->setLabel('Usuario')->validate());
		$formSMTP->addElement(Input::create('configuracao[smtp_senha]')->setLabel('Senha')->setType('password')->validate());
		$formSMTP->addElement(Input::create('salvar')->setType('submit')->setValue('Salvar')->addClass(array('btn','btn-primary')));

		$this->view->assign('formFTP', $formFTP);
		$this->view->assign('formSMTP', $formSMTP);
	}

	public function save($params = array())
	{
		try
		{
			$configuracao	= new \Foxframe\Service();
			$configuracao->save($params['configuracao'], '\Entities\Corp\Configuracao');
			$this->em->flush();

			$this->setFlash('Success', 'Sucesso', 'Configuração salva com sucesso');
		}
		catch (\Exception $e)
		{
			$this->setFlash('Error', 'Erro ao salvar configuração', $e->getMessage());
		}
		$this->redirect(BASE_PATH.'admin/configuracao');
	}
}